<?php
/**
 * Password Rule
 * Validation for password fields
 *
 * @package     Visi
 * @subpackage  Rules
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 */
class Visi_Rule_Password extends Q_Rule_Abstract {

    /**
     * The parameters that were passed to this rule
     */
    protected $_params = array(
        'min' => 1,
        'requireOneDigit' => false,
        'requireOneUpper' => false,
        'requireOneSpecial' => false,
    );
    
    public function isValid($value) {
    
        $min = $this->getParam('min');
        $requireOneDigit = $this->getParam('requireOneDigit');
        $requireOneUpper = $this->getParam('requireOneUpper');
        $requireOneSpecial = $this->getParam('requireOneSpecial');
        $length = strlen($value);
        $constraints = array();
        if ($length < $min) {
            $constraints[] = false;
        }
        if ($requireOneDigit) {
            $constraints[] = preg_match('/[0-9]/', $value);
        }
        if ($requireOneUpper) {
            $constraints[] = preg_match('/[A-Z]/', $value);
        }
        if ($requireOneSpecial) {
            $constraints[] = preg_match('/[^0-9a-zA-Z]/', $value);
        }
        return !(in_array(false, $constraints));
    
    }

}
